﻿
class HashSet {
  private HashMap map;

  private static Object DUMMY_VALUE = new Object();
  
  public HashSet() {
    this( 16 );
  }

  public HashSet( int initialCapacity ) {
    this( initialCapacity, 0.75 );
  }
  
  public HashSet( int initialCapacity, double loadFactor ) {
    map = new HashMap( initialCapacity, loadFactor );
  }

  public HashSet( HashSet other ) {
    this.map = new HashMap( other.map );
  }

  public boolean add( Object obj ) {
    return map.put( obj, DUMMY_VALUE ) != DUMMY_VALUE;
  }

  public void clear() {
    map.clear();
  }

  public boolean contains( Object obj ) {
    return map.containsKey( obj );
  }

  public boolean isEmpty() {
    return map.isEmpty();
  }

  public boolean remove( Object obj ) {
    return map.remove( obj ) == DUMMY_VALUE;
  }

  public int size() {
    return map.size();
  }

  // Java: Not in Java
  public String toString() {
    String result = "[HashSet:";

    ArrayList elements = map.keySet();

    if ( elements.size() > 0 )
      for ( int i=0; i<elements.size(); i++ ) {
        result += " " + elements.get( i );

        if ( i < elements.size() - 1 )
          result += ",";
      }
    else
      result += " empty";

    result += "]";

    return result;
  }
}
